package com.atguigu.java.test1;

import org.junit.Test;

import java.io.*;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * 实现TCP的网络编程
 * 例题三：从客户端发送文件到服务端，服务端保存文件到本地，并返回”发送成功“ 给客户端。并关闭连接
 *
 * @author shkstart
 * @create 2021-02-01 20:58
 */
public class TCPTest3 {
    @Test
    //客户端
    public void clect() {
        Socket sc = null;
        FileInputStream f1 = null;
        OutputStream o1 = null;
        InputStream i3 = null;
        ByteArrayOutputStream bao = null;
        try {
            InetAddress i1 = InetAddress.getLocalHost();
            sc = new Socket(i1, 1404);

            f1 = new FileInputStream("Capture001.png");
            o1 = sc.getOutputStream();

            byte[] bytes = new byte[1025];
            int len;
            while ((len = f1.read(bytes)) != -1) {
                o1.write(bytes, 0, len);
            }
            System.out.println("客户端发送完成");
            sc.shutdownOutput();

            //接收一下来自服务端的消息
            i3 = sc.getInputStream();
            bao = new ByteArrayOutputStream();
            byte[] bytes1 = new byte[1023];
            int lens;
            while ((lens = i3.read(bytes1)) != -1) {
                bao.write(bytes1, 0, lens);
            }
            System.out.println(bao.toString());

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (o1 != null)
                    o1.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if (f1 != null)
                    f1.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if (sc != null)
                    sc.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if (i3 != null) {
                    i3.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if (bao != null) {
                    bao.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }

        }


    }


    @Test
    //服务端
    public void server() {
        ServerSocket s1 = null;
        Socket accept = null;
        InputStream i1 = null;
        FileOutputStream f1 = null;
        OutputStream os = null;
        try {
            s1 = new ServerSocket(1404);
            accept = s1.accept();
            System.out.println("服务器启动成功，开始监听");
            i1 = accept.getInputStream();
            f1 = new FileOutputStream("beautiful.png");

            byte[] bytes = new byte[1024];
            int len;
            while ((len = i1.read(bytes)) != -1) {
                f1.write(bytes, 0, len);
            }

//            System.out.println("接收到了");
            //服务端收到后给客户端一个反馈
            os = accept.getOutputStream();
//            os.write("你好，我收到了非常nice ".getBytes(StandardCharsets.UTF_8));

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (f1 != null)
                    f1.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if (i1 != null)
                    i1.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if (accept != null)
                    accept.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if (s1 != null)
                    s1.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if (os != null) {
                    os.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }

        }

    }
}
